package com.asiainfo.lop;

import android.content.Intent;
import android.os.Parcelable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.asiainfo.lop.net.VolleyInterface;
import com.asiainfo.lop.net.VolleyRequest;
import com.asiainfo.lop.util.CryptionUtil;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.TextChange;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

/**
 * 登录Activity
 * Created by wucm(wucm@asiainfo.com) on 2016/5/26 20:16.
 */
@EActivity(R.layout.register)
public class RegisterActivity extends BaseMainActivity {
    private UserInfo userInfo;

    @ViewById(R.id.login_user_id_tv)
    EditText userIdTv;
    @ViewById(R.id.login_user_password_tv)
    EditText userPasswordTv;
    @ViewById(R.id.login_user_id_delete)
    ImageView userIdDelete;
    @ViewById(R.id.login_user_password_delete)
    ImageView userPasswordDelete;

    @ViewById(R.id.register_phone_tv)
    EditText registerPhoneTv;
    @ViewById(R.id.register_phone_delete)
    ImageView registerPhoneDelete;

    @ViewById(R.id.login_loading)
    ProgressBar loginLoadingPb;
    @ViewById(R.id.register_btn)
    TextView register_btn;

    @StringRes(R.string.rest_url_rooturl)
    String restRootUrl;

    @AfterViews
    void init() {
        userInfo = new UserInfo(this);

        boolean isSavePassowrd = userInfo.getBooleanInfo(UserInfo.IS_SAVE_PASSWORD);
//        if (isSavePassowrd) {
        userIdTv.setText("");
        userPasswordTv.setText("");
//        }
    }

    /**
     * 用户名输入框清空图标显示
     */
    @TextChange(R.id.login_user_id_tv)
    void userIdTextChange() {
        String userId = userIdTv.getText().toString();
        if (userId.length() == 0) {
            userIdDelete.setVisibility(View.INVISIBLE);
        } else {
            userIdDelete.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 密码输入框清空图标显示
     */
    @TextChange(R.id.login_user_password_tv)
    void userPasswordTextChange() {
        String userPassword = userPasswordTv.getText().toString();
        if (userPassword.length() == 0) {
            userPasswordDelete.setVisibility(View.INVISIBLE);
        } else {
            userPasswordDelete.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 用户名输入框清空图标显示
     */
    @TextChange(R.id.register_phone_tv)
    void registerPhoneTextChange() {
        String registerPhone = registerPhoneTv.getText().toString();
        if (registerPhone.length() == 0) {
            registerPhoneDelete.setVisibility(View.INVISIBLE);
        } else {
            registerPhoneDelete.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 用户名输入框清空
     */
    @Click(R.id.login_user_id_delete)
    void userIdDelete() {
        userIdTv.setText("");
    }

    /**
     * 密码输入框清空
     */
    @Click(R.id.login_user_password_delete)
    void userPasswordDelete() {
        userPasswordTv.setText("");
    }

    /**
     * 用户名输入框清空
     */
    @Click(R.id.register_phone_delete)
    void registerPhoneDelete() {
        registerPhoneTv.setText("");
    }

    /**
     * 返回按钮
     */
    @Click(R.id.register_back_btn)
    void registerBack() {
        this.finish();
//        Intent intent = new Intent(getApplication(), LoginActivity_.class);
//        startActivity(intent);
//        System.exit(0);
    }


    /**
     * 登录
     */
    @Click(R.id.register_btn)
    void register_btn() {
        String userId = userIdTv.getText().toString().trim();
        String userPassword = userPasswordTv.getText().toString().trim();
        String phone = registerPhoneTv.getText().toString().trim();

        if (userId.length() == 0) {
            Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (userPassword.length() == 0) {
            Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phone.length() == 0) {
            Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (phone!=null && !phone.matches("^1[3|4|5|7|8]\\d{9}$")) {
            Toast.makeText(RegisterActivity.this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
            return;
        }


        userId = userId.toUpperCase();

        userId = CryptionUtil.encryptWebParam(userId);
        userPassword = CryptionUtil.encryptWebParam(userPassword);
        phone = CryptionUtil.encryptWebParam(phone);

        String loginUrl = "/login/register/@userId@/@userPassword@/@phone@";
        loginUrl = loginUrl.replace("@userId@", userId);
        loginUrl = loginUrl.replace("@userPassword@", userPassword);
        loginUrl = loginUrl.replace("@phone@", phone);
        loginUrl = restRootUrl + loginUrl;

        loginGet(loginUrl);
//        this.finish();
    }

    private void loginGet(String url) {
        loginLoadingPb.setVisibility(View.VISIBLE);
        register_btn.setVisibility(View.INVISIBLE);
        VolleyInterface volleyInterface = new VolleyInterface(this) {
            @Override
            public void success(JSONObject result) {
                register_btn.setVisibility(View.VISIBLE);
                loginLoadingPb.setVisibility(View.INVISIBLE);

                flag = result.getString("flag");
                reason = result.getString("reason");
                data = result.getString("data");
                if (!RestResultInfo.RESULT_SUCCESS_FLAG.equals(flag)) {
                    Toast.makeText(RegisterActivity.this, reason, Toast.LENGTH_LONG).show();
                    return;
                }
                if (data !=null && data.equals("true")){
                    Intent intent = new Intent(getApplication(), LoginActivity_.class);
                    startActivity(intent);
//                    System.exit(0);

                    Toast.makeText(RegisterActivity.this, reason, Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
                    return;

                }

            }

            @Override
            public void error(VolleyError volleyError) {
                register_btn.setVisibility(View.VISIBLE);
                loginLoadingPb.setVisibility(View.INVISIBLE);
            }
        };
        VolleyRequest.get(this, url, "LoginActivity", volleyInterface);
    }

    /**
     * 检查更新
     */
    @AfterViews
    void checkUpdate() {
        UpdateManager manager = new UpdateManager(this);
        // 检查软件更新
//        manager.checkUpdate();
    }

    @AfterViews
    void createShortCut() {
        Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutintent.putExtra("duplicate", false);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.logo);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), CameraActivity_.class));
        sendBroadcast(shortcutintent);
    }
}
